/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.accident.service.impl;

import com.koron.accident.domain.WorkTime;
import com.koron.accident.mapper.WorkTimeMapper;
import com.koron.accident.service.api.WorkTimeService;
import com.koron.accident.util.BusinessCode;
import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkTimeServiceImpl implements WorkTimeService {

    private final static  Logger logger= LoggerFactory.getLogger(WorkTimeServiceImpl.class);
    @Override
    public MessageBean<Integer> save(WorkTime bean) {
        MessageBean<Integer> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(bean.getCorpCode())){
                msg.setCode(BusinessCode.WORKTIME_EMPTY_CORPCODE.getCode());
                msg.setDescription(BusinessCode.WORKTIME_EMPTY_CORPCODE.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(bean.getDepartmentCode())||StringUtils.isEmpty(bean.getDepartmentName())){
                msg.setCode(BusinessCode.WORKTIME_EMPTY_DEPARTMENTCODE.getCode());
                msg.setDescription(BusinessCode.WORKTIME_EMPTY_DEPARTMENTCODE.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(bean.getYears())){
                msg.setCode(BusinessCode.WORKTIME_EMPTY_YEARS.getCode());
                msg.setDescription(BusinessCode.WORKTIME_EMPTY_YEARS.getDescription());
                return msg;
            }
//            if (null==bean.getDays()||bean.getDays()<=0){
//                msg.setCode(BusinessCode.WORKTIME_EMPTY_DAYS.getCode());
//                msg.setDescription(BusinessCode.WORKTIME_EMPTY_DAYS.getDescription());
//                return msg;
//            }
//            if (null==bean.getDayWorkTimes()||bean.getDayWorkTimes()<=0){
//                msg.setCode(BusinessCode.WORKTIME_EMPTY_DAYWORKTIMES.getCode());
//                msg.setDescription(BusinessCode.WORKTIME_EMPTY_DAYWORKTIMES.getDescription());
//                return msg;
//            }
            if (null==bean.getTotalWorkTimes()){
                msg.setCode(BusinessCode.WORKTIME_EMPTY_TOTALWORKTIES.getCode());
                msg.setDescription(BusinessCode.WORKTIME_EMPTY_TOTALWORKTIES.getDescription());
                return msg;
            }

//            if (null==bean.getStaffNumber()||bean.getStaffNumber()<=0){
//                msg.setCode(BusinessCode.WORKTIME_EMPTY_STAFFNUMBER.getCode());
//                msg.setDescription(BusinessCode.WORKTIME_EMPTY_STAFFNUMBER.getDescription());
//                return msg;
//            }
            Integer value=0;
            if (StringUtils.isEmpty(bean.getId())){
                //新增
                bean.setAddTime(bean.getUpdateTime());
                bean.setAddUser(bean.getUpdateUser());
                bean.setAddUserName(bean.getUpdateUserName());
                bean.setId(DefaultIdGenerator.getInstance().generateLongId());
                value=factory.getMapper(WorkTimeMapper.class).insert(bean);
            }else{
                //修改
                WorkTime entity=queryOne(bean.getId()).getData();
                if (null==entity){
                    msg.setCode(BusinessCode.WORKTIME_NOT_EXIST.getCode());
                    msg.setDescription(BusinessCode.WORKTIME_NOT_EXIST.getDescription());
                    return msg;
                }
                value=factory.getMapper(WorkTimeMapper.class).update(bean);
            }
            if (value>0){
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription("工时记录保存成功");
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("工时记录保存失败");
                msg.setData(value);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> delete(String id) {
        MessageBean<Integer> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(id)){
                msg.setCode(BusinessCode.WORKTIME_NOT_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.WORKTIME_NOT_CHANGE_DATA.getDescription());
                return msg;
            }
            WorkTime entity=queryOne(id).getData();
            if (null==entity){
                msg.setCode(BusinessCode.WORKTIME_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.WORKTIME_NOT_EXIST.getDescription());
                return msg;
            }
            Integer value=factory.getMapper(WorkTimeMapper.class).delete(id);
            if (value>0){
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription("工时记录删除成功");
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("工时记录删除失败");
                msg.setData(value);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<WorkTime> queryOne(String id) {
        MessageBean<WorkTime> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(id)){
                msg.setCode(BusinessCode.WORKTIME_NOT_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.WORKTIME_NOT_CHANGE_DATA.getDescription());
                return msg;
            }
            WorkTime entity=factory.getMapper(WorkTimeMapper.class).queryOne(id);
            msg.setData(entity);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription("获取工时记录详情成功");
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<DataBean<WorkTime>> list(String corpCode, String departmentCode, String startYears, String endYears, Integer start, Integer rows) {
        MessageBean<DataBean<WorkTime>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            List<String> years=new ArrayList<>();
            getYerMonths(years,startYears,endYears);
            Integer totalNumber=0;
            List<WorkTime> list=new ArrayList<>();
            if (null!=years&&years.size()>0){
                list=factory.getMapper(WorkTimeMapper.class).queryByCondition(corpCode, departmentCode, years, start, rows);
                totalNumber=factory.getMapper(WorkTimeMapper.class).countByCondition(corpCode, departmentCode, years);
            }else{
                list=factory.getMapper(WorkTimeMapper.class).queryByCondition(corpCode, departmentCode, null, start, rows);
                totalNumber=factory.getMapper(WorkTimeMapper.class).countByCondition(corpCode, departmentCode, null);
            }
            rows = rows == null ?totalNumber:( rows <= 0 ? 1 : rows);
            Integer totalPage=(totalNumber>rows)?(totalNumber%rows==0?totalNumber/rows:totalNumber/rows+1):1;
            DataBean<WorkTime> dataBean=new DataBean<>();
            dataBean.setList(list);
            dataBean.setTotalNumber(totalNumber);
            dataBean.setTotalPage(totalPage);
            dataBean.setPageSize(rows);
            msg.setData(dataBean);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription("获取工时列表成功");
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    private List<String> getYerMonths(List<String> years,String startYears,String endYears){
        if (StringUtils.isNotEmpty(startYears)){
            int startyear = Integer.parseInt(startYears.substring(0,4));
            int startMonth=Integer.parseInt(startYears.substring(4,6));
            if (StringUtils.isNotEmpty(endYears)){
                int endYear=Integer.parseInt(endYears.substring(0,4));
                int endMonth=Integer.parseInt(endYears.substring(4,6));
                if (startyear!=endYear){
                    for(int j=startMonth;j<=12;j++){
                        if (j<10){
                            years.add(startyear+"0"+j+"");
                        }else{
                            years.add(""+startyear+j);
                        }
                    }
                    for(int i=startyear+1;i<endYear;i++){
                        for(int k=1;k<=12;k++){
                            if (k<10){
                                years.add(startyear+"0"+k+"");
                            }else{
                                years.add(""+startyear+k);
                            }
                        }
                    }
                    for(int k=1;k<=endMonth;k++){
                        if (k<10){
                            years.add(startyear+"0"+k+"");
                        }else{
                            years.add(""+startyear+k);
                        }
                    }
                }else{
                    for(int j=startMonth;j<=endMonth;j++){
                        if (j<10){
                            years.add(startyear+"0"+j+"");
                        }else{
                            years.add(""+startyear+j);
                        }
                    }
                }
            }else{
                years.add(startYears);
            }
        }
        return years;
    }
}
